package com.qinggeng.project.system.service.impl;

import com.qinggeng.common.enums.MessageTypeEnum;
import com.qinggeng.common.enums.UserConsumeEnum;
import com.qinggeng.common.exception.CustomException;
import com.qinggeng.common.utils.DateUtils;
import com.qinggeng.project.system.domain.UserConsume;
import com.qinggeng.project.system.domain.UserMessage;
import com.qinggeng.project.system.domain.UserWallet;
import com.qinggeng.project.system.domain.UserWithdraw;
import com.qinggeng.project.system.mapper.UserWithdrawMapper;
import com.qinggeng.project.system.service.IUserConsumeService;
import com.qinggeng.project.system.service.IUserMessageService;
import com.qinggeng.project.system.service.IUserWalletService;
import com.qinggeng.project.system.service.IUserWithdrawService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 用户提现Service业务层处理
 *
 * @author qinggeng
 * @date 2020-06-30
 */
@Service
public class UserWithdrawServiceImpl implements IUserWithdrawService {
    @Resource
    private UserWithdrawMapper userWithdrawMapper;
    @Autowired
    private IUserWalletService userWalletService;
    @Autowired
    private IUserConsumeService userConsumeService;
    @Autowired
    private IUserMessageService userMessageService;

    /**
     * 查询用户提现
     *
     * @param withdrawId 用户提现ID
     * @return 用户提现
     */
    @Override
    public UserWithdraw selectUserWithdrawById(Long withdrawId) {
        return userWithdrawMapper.selectUserWithdrawById(withdrawId);
    }

    /**
     * 查询用户提现列表
     *
     * @param userWithdraw 用户提现
     * @return 用户提现
     */
    @Override
    public List<UserWithdraw> selectUserWithdrawList(UserWithdraw userWithdraw) {
        return userWithdrawMapper.selectUserWithdrawList(userWithdraw);
    }

    /**
     * 新增用户提现
     *
     * @param userWithdraw 用户提现
     * @return 结果
     */
    @Override
    public int insertUserWithdraw(UserWithdraw userWithdraw) {
        userWithdraw.setCreateTime(DateUtils.getNowDate());
        return userWithdrawMapper.insertUserWithdraw(userWithdraw);
    }

    /**
     * 修改用户提现
     *
     * @param userWithdraw 用户提现
     * @return 结果
     */
    @Override
    public int updateUserWithdraw(UserWithdraw userWithdraw) {
        userWithdraw.setUpdateTime(DateUtils.getNowDate());
        return userWithdrawMapper.updateUserWithdraw(userWithdraw);
    }

    /**
     * 批量删除用户提现
     *
     * @param withdrawIds 需要删除的用户提现ID
     * @return 结果
     */
    @Override
    public int deleteUserWithdrawByIds(Long[] withdrawIds) {
        return userWithdrawMapper.deleteUserWithdrawByIds(withdrawIds);
    }

    /**
     * 删除用户提现信息
     *
     * @param withdrawId 用户提现ID
     * @return 结果
     */
    @Override
    public int deleteUserWithdrawById(Long withdrawId) {
        return userWithdrawMapper.deleteUserWithdrawById(withdrawId);
    }

    @Override
    public int addWithdraw(Long userId, BigDecimal price) {
        UserWallet userWallet = userWalletService.selectUserWalletByUserId(userId);
        if (userWallet.getTotalPrice().compareTo(price) < 0) {
            throw new CustomException("余额不足");
        }

        UserWithdraw userWithdraw = new UserWithdraw();
        userWithdraw.setPrice(price);
        userWithdraw.setUserId(userId);
        int result = insertUserWithdraw(userWithdraw);

        userWallet.setTotalPrice(userWallet.getTotalPrice().subtract(price));
        userWallet.setFreezePrice(userWallet.getFreezePrice().add(price));
        userWalletService.updateUserWallet(userWallet);

        //收支明细中添加提现记录
        userConsumeService.addUserConsume(userId,price,userWithdraw.getWithdrawId(),UserConsumeEnum.用户提现,"-");

        //发送消息
        UserMessage userMessage = new UserMessage();
        userMessage.setMessageType(MessageTypeEnum.零钱消息.getValue());
        userMessage.setUserId(userId);
        userMessage.setFkId(userWithdraw.getWithdrawId());
        userMessage.setFkMark("余额提现申请");
        userMessageService.insertUserMessage(userMessage);

        return result;
    }
}
